//
//  CYYNetWorkManager+SponsorNewParty.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+SponsorNewParty.h"

@implementation CYYNetWorkManager (SponsorNewParty)
//活动的名称 partyTitle      如：@"小明"
//活动的开始时间 partyBeginTime  如：@"2015/09/05"
//活动的结束时间 partyEndTime  如：@"2015/09/08"
//活动的地址 partyPositionName   如：@"孵化园"
//活动的地图选点坐标 partyPositionCoordinate  @80,80 **改属性的数据类型  BmobGeoPoint
//活动详情 partyDetailText  如：@"和嘎哈哈哈撒谎时肯定会"
//活动的发起者 partySponsor  如：@"小明"
//活动的暗号 secretCode  如：@"123456"
#pragma mark - 方法1
//发起活动：活动标题，活动开始时间，活动结束时间，活动位置名字，活动位置坐标，活动详情，活动发起者，活动的暗码
//活动的发起者根据 登陆的成功读取本地缓存判断  [CYYNetWorkManager currentUserGet]
/*
   发布活动默认成为参与者
 */
+ (void)sponsorNewPartyWithPartyTitle:(NSString *)partyTitle
                       partyBeginTime:(NSString *)partyBeginTime
                         partyEndTime:(NSString *)partyEndTime
                    partyPositionName:(NSString *)partyPositionName
              partyPositionCoordinate:(BmobGeoPoint *)partyPositionCoordinate
                      partyDetailText:(NSString *)partyDetailText
                         partySponsor:(BmobUser *)partySponsor
                      partySecretCode:(NSString *)partySecretCode
                               sucess:(Sucess)sucess
                                 fail:(Fail)fail{
    if ([partyTitle  isEqual: @""] || [partyBeginTime  isEqual: @""] || [partyEndTime  isEqual: @""] || [partyPositionName  isEqual: @""] || [partySecretCode  isEqual: @""]) {
        NSError * error = [[NSError alloc]initWithDomain:@"输入的信息不能为空" code:0 userInfo:nil];
        fail(error);
    }else{
        BmobObject * newParty = [BmobObject objectWithClassName:@"Parties"];
        [newParty setObject:partyTitle forKey:@"partyTitle"];
        [newParty setObject:partyBeginTime forKey:@"partyBeginTime"];
        [newParty setObject:partyEndTime forKey:@"partyEndTime"];
        [newParty setObject:partyPositionName forKey:@"partyPositionName"];
        [newParty setObject:partyPositionCoordinate forKey:@"partyPositionCoordinate"];
        [newParty setObject:partyDetailText forKey:@"partyDetailText"];
        [newParty setObject:partySponsor forKey:@"partySponsor"];
        [newParty setObject:partySecretCode forKey:@"partySecretCode"];
        [newParty setObject:@{@"partySponsorName":[[CYYNetWorkManager currentUserGet] objectForKey:@"name"],@"partySponsorMobilePhone":[CYYNetWorkManager currentUserGet].mobilePhoneNumber,@"partySponsorObjectId":[CYYNetWorkManager currentUserGet].objectId} forKey:@"partySponsorInfo"];
        
        //新建relation对象
        BmobRelation *relation = [[BmobRelation alloc] init];
        [relation addObject:[BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[CYYNetWorkManager currentUserGet].objectId]];
      
        [newParty addRelation:relation forKey:@"partyParticipators"];
        
        //把当前用户放到活动所有人的属性当中
        [newParty addRelation:relation forKey:@"partyAllPeople"];
//        [newParty addRelation:relation forKey:@"partyHistory"];
        [CYYNetWorkManager compareSecretCodeWithSecretCode:partySecretCode sucess:^(NSInteger sucessNumber) {
            if (sucessNumber > 0) {
                NSError * error = [[NSError alloc]initWithDomain:@"暗号已经存在" code:123123 userInfo:nil];
                fail(error);
            }else{
            [newParty saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    sucess(@"活动发布成功");
                }else{
                    fail(error);
                }
            }];
            };
        } fail:^(NSError *error) {
            fail(error);
        }];
    }
}

#pragma mark - 方法2
/*
 //活动的名称 partyTitle      如：@"小明"
 //活动的开始时间 partyBeginTime  如：@"2015/09/05"
 //活动的结束时间 partyEndTime  如：@"2015/09/08"
 //活动的地址 partyPositionName   如：@"孵化园"
 //活动的地图选点坐标 partyPositionCoordinate  @80,80 **改属性的数据类型  BmobGeoPoint
 //活动详情 partyDetailText  如：@"和嘎哈哈哈撒谎时肯定会"
 //活动的发起者 partySponsor  如：@"小明"
 //活动的暗号 secretCode  如：@"123456"
*/

//***注意需要把 发起者 同时设置为参与者
+ (void)sponsorNewPartyWithPartyParametersDictionary:(NSDictionary *)partyParametersDictionary
                                              sucess:(Sucess)sucess
                                                fail:(Fail)fail{
    BmobObject * newParty = [BmobObject objectWithClassName:@"Parties"];
    for (NSString * key in partyParametersDictionary.allKeys) {
        [newParty setObject:[partyParametersDictionary objectForKey:key] forKey:key];
    }
}


@end
